﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using MathematicallySafe.HeadCounter.Data;
using MathematicallySafe.HeadCounter.Data.Common;
using MathematicallySafe.HeadCounter.Services;
using StructureMap;

namespace MathematicallySafe.HeadCounter.Dialogs
{
    public class NewRaiderQueryController: DialogControllerBase
    {
        private static readonly IHeadCounterService HeadCounterService = ObjectFactory.GetInstance<IHeadCounterService>();

        private List<string> _possibleAltNames;
        private bool _isNew;
        private bool _isNewMain;
        private string _altName;

        public string Name { get; private set; }
        public CharacterClass Class { get; private set; }
        public CharacterRace Race { get; private set; }
        public DateTime Joined { get; private set; }

        public NewRaiderQueryController(string name, CharacterClass characterClass, CharacterRace characterRace, DateTime joined)
        {
            Name = name;
            Class = characterClass;
            Race = characterRace;
            Joined = joined;
            IsNew = true;

            InitialiseAltList();
        }

        private void InitialiseAltList()
        {
            _possibleAltNames = new List<string>();

            var otherRaiders = from r in HeadCounterService.Guild.ActiveRaidersByMain
                               where !r.HasCharacter(Name)
                               select r;

            otherRaiders.ForEach(r => _possibleAltNames.Add(r.MainName));
        }

        public bool IsNew
        {
            get { return _isNew; }
            set
            {
                _isNew = value;
                OnPropertyChanged("IsNew");
                OnPropertyChanged("IsAlt");
            }
        }

        public bool IsAlt
        {
            get { return !IsNew; }
            set
            {
                IsNew = !_isNew;
            }
        }

        public string AltName
        {
            get { return _altName; }
            set
            {
                _altName = value;
                OnPropertyChanged("AltName");
            }
        }

        public bool IsNewMain
        {
            get { return _isNewMain; }
            set
            {
                _isNewMain = value;
                OnPropertyChanged("IsNewMain");
            }
        }

        public bool IgnoreRaider { get; private set; }

        public IEnumerable<string> PossibleAltNames
        {
            get { return _possibleAltNames; }
        }

        protected override void Ok()
        {
            if (IsAlt)
            {
                var raider = HeadCounterService.Guild.FindRaider(AltName);
                raider.AddCharacter(Name, Class, Race);

                if (IsNewMain)
                {
                    raider.SetMain(Name);
                }
            }
            else
            {
                HeadCounterService.Guild.AddRaider(Name, Class, Race, Joined);    
            }
        }

        protected override void Cancel()
        {
            IgnoreRaider = true;
        }
    }
}
